package com.onegene.pdf.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.mail.MailUtil;
import com.onegene.pdf.component.config.CommonParam;
import com.onegene.pdf.component.entity.Result;
import com.onegene.pdf.job.support.ExportParam;
import com.onegene.pdf.job.support.PdfDesc;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author: laoliangliang
 * @description: 导出PDF任务
 * @create: 2020/7/21 15:20
 **/
@Component
@Slf4j
@ConditionalOnProperty(prefix = "pdf.job", value = "enabled")
public class ExportPdfJob {

    @Autowired
    private EverydayExport everyPdfExport;

    @Autowired
    private EverydayBatchExport everydayBatchExport;

    @Autowired
    private CommonParam commonParam;

    /**
     * 每天定时导出特定报告 {@link EverydayExport}
     * 每天定时导出特定批量渠道报告 {@link EverydayBatchExport}
     */
    @Scheduled(cron = "0 30 8 * * ?")
    public void everyPdf() {
        ExportParam exportParam = new ExportParam();
        Result<List<PdfDesc>> r1 = everyPdfExport.run(exportParam);
//        Result<List<PdfDesc>> r2 = everydayBatchExport.run(exportParam);
        int sumCount = r1.getData().size();
//                + r2.getData().size();
        if (sumCount == 0) {
            String[] toList = commonParam.toMail.split(",");
            MailUtil.send(Arrays.asList(toList), DateUtil.formatDate(new Date()) + "验收PDF报告", "<h1>今日无PDF需要验收</h1>", true);
        }
    }

    @Scheduled(cron = "0 0/1 * * * ?")
    public void heart() {
        log.info("heart active");
    }

}
